package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.system.Role;
import com.ihrm.domain.system.response.RoleResult;
import com.ihrm.system.service.RoleService;
import javafx.print.PaperSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sys")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;
    
    /**
     * 分配权限
     */
    @RequestMapping(value = "/role/assignPrem",method = RequestMethod.PUT)
    public Result assignPrem(@RequestBody Map<String,Object> map){
        /**
         * 获取被分配的角色的id
         */
        String roleId = (String) map.get("id");
        /**
         * 获取到权限的id列表
         */
        List<String> permIds = (List<String>) map.get("permIds");
        /**
         * 调用service完成权限分配
         */
        roleService.assignPerms(roleId,permIds);
        return new Result(ResultCode.SUCCESS);
    }
    
    /**
     * 添加角色
     */
    @RequestMapping(value = "/role",method = RequestMethod.POST)
    public Result add(@RequestBody Role role) throws Exception{
        role.setCompanyId(companyId);
        roleService.save(role);
        return Result.SUCCESS();
    }
    
    /**
     * 更新角色
     */
    @RequestMapping(value = "/role/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable(name = "id") String id,@RequestBody Role role) throws Exception{
        roleService.update(role);
        return Result.SUCCESS();
    }
    
    /**
     * 删除角色
     */
    @RequestMapping(value = "/role/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable(name = "id") String id) throws Exception{
        roleService.delete(id);
        return Result.SUCCESS();
    }
    
    /**
     * 根据ID获取角色信息
     */
    @RequestMapping(value = "/role/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id) throws Exception{
        Role role = roleService.findById(id);
        RoleResult roleResult = new RoleResult(role);
        return new Result(ResultCode.SUCCESS,roleResult);
    }
    
    /**
     * 分页查询角色
     */
    @RequestMapping(value = "/role",method = RequestMethod.GET)
    public Result findByPage(int page,int pageSize,Role role) throws Exception{
        Page<Role> rolePage = roleService.findByPage(companyId, page, pageSize);
        PageResult<Role> pageResult = new PageResult(rolePage.getTotalElements(), rolePage.getContent());
        return new Result(ResultCode.SUCCESS,pageResult);
    }
    
    @RequestMapping(value = "/role/list",method = RequestMethod.GET)
    public Result findAll() throws Exception{
        List<Role> list = roleService.findAll(companyId);
        return new Result(ResultCode.SUCCESS,list);
    }
    
}
